import Vue from 'vue'
const funs = {
	secondToHour(second) {
	    let dd, hh, mm, ss;
	    second = typeof second === "string" ? parseInt(second) : second;
	    if (!second || second < 0) {
	      return 0;
	    }
	    // 天
	    dd = (second / (24 * 3600)) | 0;
	    second = Math.round(second) - dd * 24 * 3600;
	    // 小时
	    hh = (second / 3600) | 0;
	    second = Math.round(second) - hh * 3600;
	    // 分
	    mm = (second / 60) | 0;
	    // 秒
	    ss = Math.round(second) - mm * 60;
	    if (Math.round(dd) < 10) {
	      dd = dd > 0 ? "0" + dd : "00";
	    }
	    if (Math.round(hh) < 10) {
	      hh = "0" + hh;
	    }
	    if (Math.round(mm) < 10) {
	      mm = "0" + mm;
	    }
	    if (Math.round(ss) < 10) {
	      ss = "0" + ss;
	    }
	    let result = ss + "秒";
	    if (mm !== "00") {
	      result = mm + "分" + result;
	    }
	    if (hh !== "00") {
	      result = hh + "时" + result;
	    }
	    if (dd !== "00") {
	      result = dd + "天" + result;
	    }
	    return result;
	  },
	// 手机号码
	isCorrectMobile(mobile) {
		if (!mobile) {
			return false
		}
		if (!/^1[23456789]\d{9}$/.test(mobile.trim())) {
			return false;
		}
		return true;
	},
	// 身份证号码
	isCorrectPersonId(personId) {
		if (!personId) {
			return false
		}
		let p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		if (!p.test(personId.trim())) {
			return false;
		}
		return true;
	},

	getStringLenghts(str) {
		var en = 0,
			cn = 0,
			len = str.length,
			charCode = -1;
		for (let i = 0; i < len; i++) {
			charCode = str.charCodeAt(i);
			if (charCode > 0 && charCode <= 128) en += 1;
			else cn += 1;
		}
		return {
			en: en,
			cn: cn
		};
	},
	// 计算字节长度
	getCharLength(str) {
		var iLength = 0; // 记录字符的字节数
		for (var i = 0; i < str.length; i++) {
			// 遍历字符串中的每个字符
			if (str.charCodeAt(i) > 255) {
				// 如果当前字符的编码大于255
				iLength += 2; // 所占字节数加2
			} else {
				iLength += 1; // 否则所占字节数加1
			}
		}
		return iLength; // 返回字符所占字节数
	},
	//生成唯一编码
	uuid() {
		let s = [];
		let hexDigits = "0123456789abcdef";
		for (let i = 0; i < 36; i++) {
			s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
		}
		s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
		s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
		s[8] = s[13] = s[18] = s[23] = "-";

		let uuid = s.join("");
		return uuid;
	},
	toLocalDate(time) {
		let date = new Date(time);

		let month = date.getMonth() + 1;
		if (month < 10)
			month = "0" + month;
		let hours = date.getHours();
		if (hours < 10)
			hours = "0" + hours;
		let minutes = date.getMinutes();
		if (minutes < 10)
			minutes = "0" + minutes;
		let result = date.getFullYear() + "-" + month + "-" + date.getDate();
		return result;
	},
	//'2020-12-19T15:44:29+08:00' => '2020-12-19 15:44:29'
	toLocalTime(time) {
		let date = new Date(time);

		let month = date.getMonth() + 1;
		if (month < 10)
			month = "0" + month;
		let day = date.getDate();
		if (day < 10)
			day = "0" + day;
		let hours = date.getHours();
		if (hours < 10)
			hours = "0" + hours;
		let minutes = date.getMinutes();
		if (minutes < 10)
			minutes = "0" + minutes;

		let seconds = date.getSeconds();
		if (seconds < 10)
			seconds = "0" + seconds;
		let result = date.getFullYear() + "-" + month + "-" + day +
			" " + hours + ":" + minutes + ":" + seconds;
		return result;
	},
	toDayStart_RF3339(param) {
		return param ? param + "T00:00:00+08:00" : undefined;
	},
	toDayEnd_RF3339(param) {
		return param ? param + "T23:59:59+08:00" : undefined;
	},
	getDuration(start, end) {
		let d1 = new Date(start).getTime()
		let d2 = new Date(end).getTime()
		return d2 - d1
	},
	// 倒计时（天时分秒）
	getFormateTimeStamp(timeStamp) {
		let day;
		let hour;
		let min;
		let seconds;
		let isFinish = null //判断是否已结束
		if (timeStamp > 0) {
			isFinish = false
			// 天
			day = Math.floor(timeStamp / 1000 / 60 / 60 / 24)
			// 时
			let h = Math.floor(timeStamp / 1000 / 60 / 60 % 24)
			hour = h < 10 ? '0' + h : h
			// 分
			let m = Math.floor(timeStamp / 1000 / 60 % 60)
			min = m < 10 ? '0' + m : m
			// 秒
			let s = Math.floor(timeStamp / 1000 % 60)
			seconds = s < 10 ? '0' + s : s
		} else {
			isFinish = true
			day = 0
			hour = '00'
			min = '00'
			seconds = '00'
		}
		const restStamp = {
			isFinish: isFinish,
			day: day,
			hour: hour,
			min: min,
			seconds: seconds
		}
		return restStamp
	},
	downloadImage(id,callback){
		Vue.prototype.$http.post('common/file/download', {
			id: id
		}).then(res => {		
			if (res.code == 10000) {
				callback(res.data)
			}
		})
	}

}
export default funs
